<?
include("../webkit/include/simple_db.php");
include("session.php");
include("common.php");
include("ajax_base.php");

$db = new SimpleDB();

function echo_header($filename) {
	 header("Content-type: application/octet-stream"); 
	 header("Content-Disposition: attachment; filename=".iconv("utf-8","gbk",$filename));
	 header("Pragma: no-cache");
	 header("Expires: 0");  
}


if(isset($_GET['dl_excel'])) {
	echo_header("湖南电信外包呼叫中心SI结算明细表_新太科技_".$_SESSION['report_date_range'].".xls");
}


$date_range = split("_", $_SESSION['report_date_range']);

$month_total = "IF(order_date < STR_TO_DATE('".$date_range[0]."', '%Y-%m-%d'),  "; // 比较起始时间和签约时间; 2014/4/28
$month_total .= "ROUND(DATEDIFF(STR_TO_DATE('".$date_range[1]."', '%Y-%m-%d'), STR_TO_DATE('".$date_range[0]."', '%Y-%m-%d'))/30), "; // 计算时间范围内的月份
$month_total .= "ROUND(DATEDIFF(STR_TO_DATE('".$date_range[1]."', '%Y-%m-%d'), order_date)/30))"; // 计算从签约时间到截时间的月份

$sql = "select client_name, sum(order_count) as agent_sum, count(id) as client_count, order_price, order_date, client_status, service_type, sum(order_count*order_price*$month_total) as total_sum, sum(CASE client_source WHEN 1 THEN 0.3 ELSE 0.6 END * order_count*order_price*$month_total) as si_sum, $month_total as month_total, 
(CASE client_source WHEN 1 THEN 0.3 ELSE 0.6 END) as money_rate, sum(deve_price) as deve_sum, client_type from hncc_client group by client_name having 1=1 order by order_date";

//$condition = "client_status=1 and order_date >= '".$date_range[0]."' and order_date < '".$date_range[1]."'";
$condition = "client_status=1 and order_date < '".$date_range[1]."'"; // 这里只要判断签约日期在限之内即可; 2014/3/28

$sql = str_replace("1=1", $condition, $sql);
$db->query($sql);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../webkit/utils/jquery.js"></script>
<link type="text/css" rel="stylesheet" href="page.css"/>
<style> td {font-size:15px; font-family:黑体} </style>
</head>
<body  style="margin:3px; overflow-y:scroll">

<table width=100% height=100% border=1 cellspacing=0 cellpadding=5 bgcolor="#999999">
	<tr bgcolor="#eeeeee" height=40>
	<td style="font-size:25px;" colspan=10>
		湖南电信外包呼叫中心SI结算明细表  　 
		<? if(empty($_GET['dl_excel'])) { ?>
		<span id="page_links">
		<a href="<?echo $PHP_SELF."?dl_excel=1";?>">下载总表格</a>　<a href="javascript:$('#page_links').hide(); window.print()">打印本页</a>
		</span>
		<?}?>
    </td>
	</tr>
	<tr bgcolor='white' style='font-weight:bold'><td>客户名称</td><td>业务类别</td><td>租用数量</td><td>租用单价</td><td>结算月数</td><td>签约时间</td><td>收入金额</td><td>分成比例</td><td>分成金额</td><td><!--定制开发-->所属行业</td></tr>
	<?
		$agent_sum_all = 0;
		$total_sum_all = 0;
		$si_sum_all = 0;
		$deve_sum_all = 0;
		while($db->moveNext()) {
			echo "<tr bgcolor='white'><td>".$db->f("client_name")."</td><td>".$dic_data['service_type'][$db->f("service_type")]."</td><td>".$db->f("agent_sum")."</td><td>".$db->f("order_price")."</td><td>".$db->f("month_total")."</td><td>".$db->f("order_date")."</td><td>".money_num_format($db->f("total_sum"))."</td><td>".$db->f("money_rate")."</td><td>".money_num_format($db->f("si_sum"))."</td><td>".$db->f("client_type")."</td></tr>";
			$agent_sum_all += $db->f("agent_sum");
			$total_sum_all += $db->f("total_sum");
			$si_sum_all += $db->f("si_sum");
			$deve_sum_all += $db->f("deve_sum");
		}
	?>
	<tr bgcolor='white' style='font-weight:bold; color:red'><td>总计</td><td>&nbsp;</td><td><?echo $agent_sum_all?></td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>¥ <?echo money_num_format($total_sum_all)?></td><td>&nbsp</td><td>¥ <?echo money_num_format($si_sum_all)?></td><td><!--¥ <?echo money_num_format($deve_sum_all)?>-->&nbsp;</td></tr>
	<tr bgcolor="#eeeeee" height=30>
	<td colspan=10 style="font-size:20px;">
		签字盖章确认栏　　　
	</td>
	</tr>
	<tr bgcolor="white" height=120>
	<td colspan=10>
		甲方：湖南号百信息服务分公司　　　　　乙方：佳都新太科技股份有限公司<br><br>
		日期：　　　　　　　　　　　　　　　　日期： 　　　　　　　
	</td>
	</tr>
</table>


</body>
</html>
